//
//  RSSingleLineEditCell.m
//  iAccountBook
//
//  Created by 侯伟 on 2020/3/3.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RSSingleLineEditCell.h"
#import "NSString+Founction.h"

@interface RSSingleLineEditCell ()<UITextFieldDelegate>

/// 输入框
@property (nonatomic,weak)UITextField* textField;

/// 标题信息
@property (nonatomic,weak)UILabel* titleLb;

@end

@implementation RSSingleLineEditCell


+ (instancetype)cellWithTitle:(NSString *)title{
    RSSingleLineEditCell* cell = [RSSingleLineEditCell new];
    cell.title = title;
    return cell;
}

- (instancetype)init{
    if(self = [super initWithOrientation:MyOrientation_Vert]){
                
        [self initViews];
        
        [self initDatas];
        
        self.backgroundColor = UIColor.whiteColor;
        
        self.heightSize.min(PX(50));
        
        self.gravity = MyGravity_Vert_Center;
    }
    return self;
}

- (void)initDatas{
    
    self.titleFont = FONT_FIT(14);
     
    self.textFont = FONT_FIT(14);
     
    self.textColor = COLOR_BLACK_DARK;
    
    self.placehodler = @"请输入内容";
    
    self.showSeparator = YES;
    
    self.separatorInset = UIEdgeInsetsMake(0, 20, 0, 0);
    
    self.editable = YES;
}

- (void)initViews{
    
    MyRelativeLayout* relativeLayout = [MyRelativeLayout new];
    relativeLayout.padding = UIEdgeInsetsMake(0, 16, 0, 16);
    relativeLayout.myMargin = 0;
    relativeLayout.myHeight = MyLayoutSize.wrap;
    [self addSubview:relativeLayout];
        
    UILabel* titleLb = [UILabel new];
    titleLb.textAlignment = NSTextAlignmentLeft;
    titleLb.myLeft = 0;
    titleLb.myCenterY = 0;
    [relativeLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    UITextField* textField = [UITextField new];
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.tintColor = COLOR_THEME_A;
    textField.textAlignment = NSTextAlignmentRight;
    textField.returnKeyType =UIReturnKeyDone;
    textField.myVertMargin = 0;
    textField.leftPos.equalTo(titleLb.rightPos).offset(8);
    textField.myRight = 0;
    textField.myHeight = PX(36);
    [textField addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
    [relativeLayout addSubview:textField];
    _textField = textField;
}

#pragma mark --setter

- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLb.text = title;
    [self.titleLb sizeToFit];
}

- (void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    self.titleLb.font = titleFont;
    [self.titleLb  sizeToFit];
}

- (void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    _titleLb.textColor = titleColor;
}


- (void)setPlacehodler:(NSString *)placehodler{
    _placehodler = placehodler;
    self.textField.placeholder = placehodler;
}

- (NSString*)text{
    return self.textField.text;
}

- (void)setText:(NSString *)text{
    _textField.text = text;
}

- (void)setTextFont:(UIFont *)textFont{
    _textFont = textFont;
    self.textField.font = textFont;
}

- (void)setTextColor:(UIColor *)textColor{
    _textColor = textColor;
    self.textField.textColor = textColor;
}


- (void)setShowSeparator:(BOOL)showSeparator{
    if(showSeparator == _showSeparator)
        return;
    _showSeparator = showSeparator;
    if (showSeparator) {
        MyBorderline *bl = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE]; //边界线
        bl.headIndent = 20;  //头部缩进
        bl.tailIndent = 0;  //尾部缩进0个点。
        bl.thick = 1;        //粗细为1
        self.bottomBorderline = bl;
    }else{
        self.bottomBorderline = nil;
    }
}

- (void)setSeparatorInset:(UIEdgeInsets)separatorInset{
    _separatorInset = separatorInset;
    if(self.showSeparator && self.showSeparator){
        MyBorderline *bl = [[MyBorderline alloc] initWithColor:COLOR_GRAY_LINE]; //边界线
        bl.headIndent = separatorInset.left;  //头部缩进
        bl.tailIndent = separatorInset.right;  //尾部缩进0个点。
        bl.thick = 1;        //粗细为1
        self.bottomBorderline = bl;
    }
}

- (void)setEditable:(BOOL)editable{
    _editable = editable;
    self.textField.enabled = editable;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType{
    _keyboardType = keyboardType;
    self.textField.keyboardType = keyboardType;
}

#pragma mark -- action

- (void)textFieldValueChanged:(UITextField*)textField{
    
    if(textField.markedTextRange != NULL)
        return;
    
    NSString* text = textField.text;

    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if(self.textChangedBlock){
        self.textChangedBlock(text);
    }
}

#pragma mark -- UITextFieldDelegate
//监听return 键的点击
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
    
}
@end
